package com.ymtms.customerservice.controller;

import com.ymtms.customerservice.pojo.ComplaintInformation;
import com.ymtms.customerservice.service.ComplaintInformationService;
import com.ymtms.customerservice.service.CustomerInformationService;
import com.ymtms.customerservice.util.TimeRangeUtil;
import com.ymtms.customerservice.util.ValidateUtil;
import com.ymtms.customerservice.vo.GridRequest;
import com.ymtms.customerservice.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("/complaintinformation")
public class ComplaintInformationController {
    //用户姓名
    private static String staticName;
    //编号
    private static String staticCustomerCode;
    @Autowired
    private ComplaintInformationService complaintInformationService;

    @Autowired
    private CustomerInformationService customerInformationService;

    /**
     * 查看所有投诉信息
     * @return
     */
    @RequestMapping("/show")
    @ResponseBody
    public List<ComplaintInformation> show(String customer,String goodsBillCode,String appealDate,String ifHandle){
        System.out.println("/show");
        List<ComplaintInformation> complaintInformations = new LinkedList<>();
        complaintInformations=complaintInformationService.GetAllComplaintInformation(customer,goodsBillCode,appealDate,ifHandle);
        return  complaintInformations;
    }


    /**
     * 查看所有投诉信息
     * @return
     */
    @RequestMapping("/showall")
    @ResponseBody
    public GridResult<ComplaintInformation> cmblist(GridRequest gridRequest, /* 封装页面中laiyu表格所传过来的基本信息*/
                                               ComplaintInformation complaintInformation, /*实体类接收非范围参数*/
                                               @RequestParam(value = "sappealDate", required = false)String sappealDate,
                                               @RequestParam(value = "balanceDate", required = false) String balanceDate){
        String beginTime = "";
        String endTime = "";
        if (ValidateUtil.isNotEmpty(balanceDate)) {
            String[] split = balanceDate.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        System.out.println("gridRequest:"+gridRequest);
        System.out.println("ComplaintInformation:"+complaintInformation);
        System.out.println("balanceDate:"+balanceDate);
        GridResult<ComplaintInformation> result = complaintInformationService.queryByPage(
                gridRequest, complaintInformation,sappealDate, new TimeRangeUtil(beginTime, endTime));
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("limit")));
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(1,10);
        result.setCode(0);
        result.setMsg("");
        System.out.println(result);
        return result;
    }


    /**
     * 插入投诉信息
     * @param customer
     * @param goodsBillCode
     * @param appealContent
     * 时间下面获取，其他信息默认
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map insert(String customer,String goodsBillCode,String appealContent){
        System.out.println("/insert"+customer+":"+goodsBillCode+":"+appealContent);
        String result = null;
        String appealDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
        System.out.println(appealDate);
        result=complaintInformationService.insertComplaint(customer,goodsBillCode,appealContent,appealDate);
        Map map = new HashMap();
        map.put("msg",result);
        return map;
    }

    /**
     * 获取session中的值
     * 由电话号码查到账号及客户编号
     * @return
     */
    @RequestMapping("/getsession")
    @ResponseBody
    public Map getsession(String account){
        account = "16677767656";

        Map map=new HashMap();
        if(account.length()==11){
            //客户
            //由电话号码查找客户编号
            staticCustomerCode = customerInformationService.findCustomerCodeByPhone(account);
            //客户姓名
            staticName=customerInformationService.findNameByPhone(account);
        }else{
            //管理员
            staticName = customerInformationService.findUserNameByAccount(account);
        }

        staticName ="李希";
        map.put("customer",staticName);
        return map;
    }


    /**
     * 查找货运单主表的当前客户的货运单编号
     * 因只需要编号所以就不需要对象接收了
     * @return
     */
    @RequestMapping("/selectgoodsbillcode")
    @ResponseBody
    public List<String> selectGoodsBillCode(){
//        customerNO="9RIEU68574YR6FB4B5NMC78945X6LXLX";
        List<String> goodsBillCode = new LinkedList<>();
        goodsBillCode = complaintInformationService.findAllGoodsBillCodeByCustomerNO(staticCustomerCode);
        return goodsBillCode;
    }

    /**
     * 投诉处理1：退款
     * 退款百分比？
     * @param ciid
     * @param request
     * @return
     */
    @RequestMapping("/refund")
    @ResponseBody
    public Map refund(Integer ciid,HttpServletRequest request){
        //获取当前登录用户名字（管理员）
//        String dealPerson = (String) request.getSession().getAttribute("customer");
        Map map = new HashMap();
        String dealDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
        String result=complaintInformationService.refundComplainInformationByCiid(ciid,staticName,dealDate);
        map.put("msg",result);
        return map;
    }

    /**
     * 投诉处理2：垃圾投诉无需理会
     * @param ciid
     * @param request
     * @return
     */
    @RequestMapping("/ignore")
    @ResponseBody
    public Map ignore(Integer ciid,HttpServletRequest request){
        //获取当前登录用户名字（管理员）
//        String dealPerson = (String) request.getSession().getAttribute("customer");
        Map map = new HashMap();
        String dealDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
        String result=complaintInformationService.ignoreComplainInformationByCiid(ciid,staticName,dealDate);
        map.put("msg",result);
        return map;
    }
    /**
     * 投诉处理3：删除投诉
     * @param ciid
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(Integer ciid,HttpServletRequest request){
        //获取当前登录用户名字（管理员）
//        String dealPerson = (String) request.getSession().getAttribute("customer");
        Map map = new HashMap();
        String dealDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
        String result=complaintInformationService.delete(ciid,staticName,dealDate);
        map.put("msg",result);
        return map;
    }

    /**
     * 我的投诉历史
     * @param customer
     * @param goodsBillCode
     * @param appealDate
     * @param ifHandle
     * @return 我的投诉历史list
     */
    @RequestMapping("/mycomplaint")
    @ResponseBody
    public List<ComplaintInformation> myComplaint(String customer,String goodsBillCode,String appealDate,String ifHandle){
        //staticName
        List<ComplaintInformation> complaintInformations =complaintInformationService.GetAllComplaintInformation(staticName,goodsBillCode,appealDate,ifHandle);
        return complaintInformations;
    }

}
